﻿@model CompanyManagementDLL.MetaData.ProjectMetaData

@{
    ViewBag.Title = "Edit Project";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/js/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/js/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/js/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

<div class="row">
    <div class="col-md-9">
        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">Edit Project</h3>
            </div>

            <form method="post" id="frmedit">
                @Html.HiddenFor(model => model.PorjectID)
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-5">
                            <div style="color: red">
                                <b>@Html.ValidationSummary()</b>
                            </div>
                            <div class="form-group">
                                <label>Project Name</label>
                                @Html.TextBoxFor(model => model.ProjectName, new { @class = "form-control", @placeholder = "Enter Project Name" })
                            </div>
                            <div class="form-group">
                                <label>StartDate</label>
                                @Html.TextBoxFor(model => model.StartDate, new { @class = "form-control date-picker", @placeholder = "Enter StartDate",@Value=Model.StartDate.Value.ToString("MM/dd/yyyy") })
                            </div>
                            <div class="form-group">
                                <label>EndDate</label>
                                @Html.TextBoxFor(model => model.EndDate, new { @class = "form-control date-picker", @placeholder = "Enter EndDate",@Value=Model.EndDate.Value.ToString("MM/dd/yyyy") })
                            </div>
                            <div class="form-group">
                                <label>Description</label>
                                @Html.TextAreaFor(model => model.Description, new { @class = "form-control", @placeholder = "Enter Description" })
                            </div>
                            <div class="checkbox">
                                <label class="">
                                    <div class="icheckbox_minimal" style="position: relative;" aria-checked="false" aria-disabled="false">
                                        @Html.CheckBoxFor(u => u.IsActive, new { style = "position: absolute; opacity: 0;" })
                                        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 0px none; opacity: 0;"></ins>
                                    </div>
                                    Is Active
                                </label>
                            </div>
                            @Html.HiddenFor(model => model.CheckUser)
                        </div>
                        <div class="col-md-4" >
                            @Html.Action("ProjectAssign", "Project")
                        </div>
                    </div>
                </div>
                <div class="box-footer">
                    <button class="btn btn-primary" type="submit" id="submit">Submit</button>
                    <a class="btn btn-danger" href="@Url.Action("Index", "Project")"><i class="fa fa-arrow-left"></i>Cancel</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $("#pagetitle").html("Project");

    $(document).ready(function () {
        var projectid = '@Model.PorjectID';
        $.ajax({
            url: "/Project/getProjectDetail?projectid=" + projectid,
            success: function (data) {
                $('.chkbxAction').each(function () {
                    for (var i = 0; i < data.length; i++) {
                        if (Number(data[i].UserID) == Number($(this).val())) {
                            $(this).iCheck('check');
                        }
                    }
                });
            }
        });
    });

    $("#submit").click(function (e) {
        e.preventDefault();
        var formstate = $("#frmedit").valid();
        var isChecked = 0;
        var CheckUser = '';
        $('.chkbxAction').each(function () {
            if ($(this).is(":checked")) {
                CheckUser += $(this).val() + ",";
                isChecked = 1;
            }
        });
        if (isChecked == 1) {
            $("#error").css("display", "none");
            CheckUser = CheckUser.slice(0, -1);
            $("#CheckUser").val(CheckUser);
            if (formstate == true) {
                var projectmetadata = {
                    PorjectID:$("#PorjectID").val(),
                    ProjectName: $("#ProjectName").val(),
                    StartDate: $("#StartDate").val(),
                    EndDate: $("#EndDate").val(),
                    IsActive: $("#IsActive").is(":checked"),
                    Description: $("#Description").val(),
                    CheckUser: $("#CheckUser").val()
                }

                $.ajax({
                    url: "/Project/Edit",
                    contentType: "application/json",
                    type: "POST",
                    datatype: "json",
                    data: JSON.stringify({ projectmetadata: projectmetadata }),
                    success: function (data) {
                        window.location = "/Project";
                    }
                });
            }
        }
        else {
            $("#error").css("display", "initial");
        }
    });
</script>
